<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\AudioApi;


class LoverRecharge extends Base
{
    protected $table = 'lover_recharge_bag_log';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    public function create($body = [], $rank = 1,$date = '')
    {

        $now_date = $date ?? date('Y-m-d', time());
        $data['user_id'] = $body['user_id'] ?? 0;
        $data['type'] = $body['type'] ?? 1;
        $data['rank'] = $rank;
        $data['recharge_value'] = $body['price'] ?? 0;
        $data['created_date'] = $now_date;
        return DB::table($this->table)->insert($data);
    }

    public function getLog($user_id, $now_date)
    {
        $where['user_id'] = $user_id;
        $where['created_date'] = $now_date;
        $res = DB::table($this->table)->where($where)->first();
        if (!$res) {
            return false;
        }
        return $res;
    }

    public function getAllLog($user_id, $field = ['user_id'])
    {
        $where['user_id'] = $user_id;
        $res = DB::table($this->table)->where($where)->select($field)->orderBy('rank','asc')->get()->toArray();
        if (!$res) {
            return [];
        }
        return $res;
    }

    public function checkUserBag($user_id, $rank)
    {
        $where['user_id'] = $user_id;
        $where['rank'] = $rank;
        return DB::table($this->table)->where($where)->where('created_date','<=',date('Y-m-d'))->first();
    }

    public function updateLog($user_id,$rank,$data = [])
    {
        //送礼物
        $where['user_id'] = $user_id;
        $where['rank'] = $rank;
        return DB::table($this->table)->where($where)->update($data);
    }

}